/*****************************************************************************
 * hal_esp32_pwm.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "hal/source/hal_esp32/hal_esp32.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "hal.pwm"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_u8 ch0 : 1;
    plum_u8 ch1 : 1;
    plum_u8 ch2 : 1;
    plum_u8 ch3 : 1;
    plum_u8 ch4 : 1;
    plum_u8 ch5 : 1;
    plum_u8 ch6 : 1;
    plum_u8 ch7 : 1;
} hal_esp32_pwm_channel_t;

typedef struct {
    plum_u32         id;
    ledc_channel_t   ch;
    struct list_head list;
} hal_esp32_pwm_hdl_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE
hal_esp32_pwm_hdl_t esp32_pwm_head;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_STATIC_INLINE
ledc_channel_t hal_esp32_pwm_channel_apply(plum_void)
{
    static hal_esp32_pwm_channel_t t_ch_flag = {};

    ledc_channel_t ch = LEDC_CHANNEL_MAX;

    if (!t_ch_flag.ch0) {
        ch            = LEDC_CHANNEL_0;
        t_ch_flag.ch0 = 1;
    }
    else if (!t_ch_flag.ch1) {
        ch            = LEDC_CHANNEL_1;
        t_ch_flag.ch1 = 1;
    }
    else if (!t_ch_flag.ch2) {
        ch            = LEDC_CHANNEL_2;
        t_ch_flag.ch2 = 1;
    }
    else if (!t_ch_flag.ch3) {
        ch            = LEDC_CHANNEL_3;
        t_ch_flag.ch3 = 1;
    }
    else if (!t_ch_flag.ch4) {
        ch            = LEDC_CHANNEL_4;
        t_ch_flag.ch4 = 1;
    }
    else if (!t_ch_flag.ch5) {
        ch            = LEDC_CHANNEL_5;
        t_ch_flag.ch5 = 1;
    }
#if (SOC_LEDC_CHANNEL_NUM > 6)
    else if (!t_ch_flag.ch6) {
        ch            = LEDC_CHANNEL_6;
        t_ch_flag.ch6 = 1;
    }
    else if (!t_ch_flag.ch7) {
        ch            = LEDC_CHANNEL_7;
        t_ch_flag.ch7 = 1;
    }
#endif

    return (ch);
}

PLUM_PRIVATE
plum_s32 hal_esp32_pwm_register(plum_u32 id, ledc_channel_t channel)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!esp32_pwm_head.list.next) {
            INIT_LIST_HEAD(&esp32_pwm_head.list);
        }

        hal_esp32_pwm_hdl_t *node =
            plum_hal_malloc(sizeof(hal_esp32_pwm_hdl_t));
        if (!node) {
            LOG_E("%s malloc err", PLUM_FUNC);
            rc = PLUM_ECODE_EMEM;
            break;
        }

        node->id = id;
        node->ch = channel;

        list_add(&node->list, &esp32_pwm_head.list);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
ledc_channel_t hal_esp32_pwm_channel(plum_u32 id)
{
    ledc_channel_t ch = LEDC_CHANNEL_MAX;

    struct list_head *   pos;
    hal_esp32_pwm_hdl_t *node = plum_null;
    list_for_each(pos, &esp32_pwm_head.list)
    {
        node = list_entry(pos, hal_esp32_pwm_hdl_t, list);
        if (node->id == id) {
            ch = node->ch;
            break;
        }
    }

    return (ch);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

/**

 * @note:   pwm固定使用定时器0
 */
plum_s32 plum_hal_pwm_init(plum_u32 id, plum_hal_pwm_cof_t *config)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_PWM(id)) {
            LOG_E("the id is not a pwm id");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (!config) {
            LOG_E("the config pointer is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        ledc_timer_config_t timer = {
            .speed_mode      = LEDC_LOW_SPEED_MODE,
            .timer_num       = LEDC_TIMER_0,
            .freq_hz         = config->freq,
            .duty_resolution = LEDC_TIMER_12_BIT,
            .clk_cfg         = LEDC_USE_APB_CLK,
        };
        rc = ledc_timer_config(&timer);
        if (rc) {
            LOG_E("ledc_timer_config err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
        LOG_D("ledc_timer_config success");

        ledc_channel_t ch = hal_esp32_pwm_channel_apply();
        LOG_D("pwm channel : %d", ch);

        ledc_channel_config_t channel = {};
        channel.gpio_num              = PLUM_HAL_GPIO_PIN(config->gpio);
        channel.speed_mode            = LEDC_LOW_SPEED_MODE;
        channel.channel               = ch;
        channel.intr_type             = LEDC_INTR_DISABLE;
        channel.timer_sel             = LEDC_TIMER_0;
        channel.duty                  = config->duty;
        channel.hpoint                = 0;
        rc                            = ledc_channel_config(&channel);
        if (rc) {
            LOG_E("ledc_channel_config err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
        LOG_E("ledc_channel_config success");

        rc = hal_esp32_pwm_register(id, ch);
        if (rc) {
            LOG_E("hal_esp32_pwm_register err,rc : %d", rc);
            break;
        }
        LOG_D("hal_esp32_pwm_register success");
    } while (0);

    return (rc);
}

plum_void plum_hal_pwm_suspend(plum_u32 id)
{

    do {
        if (!PLUM_HAL_ID_IS_PWM(id)) {
            LOG_E("the id is not a pwm id");
            break;
        }

        ledc_channel_t ch = hal_esp32_pwm_channel(id);
        if (ch == LEDC_CHANNEL_MAX) {
            LOG_E("the pwm id not register");
            break;
        }

        ledc_timer_pause(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0);
    } while (0);
}

plum_void plum_hal_pwm_resume(plum_u32 id)
{
    do {
        if (!PLUM_HAL_ID_IS_PWM(id)) {
            LOG_E("the id is not a pwm id");
            break;
        }

        ledc_channel_t ch = hal_esp32_pwm_channel(id);

        if (ch == LEDC_CHANNEL_MAX) {
            LOG_E("the pwm id not register");
            break;
        }

        ledc_timer_resume(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0);
    } while (0);
}

plum_s32 plum_hal_pwm_duty_get(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_PWM(id)) {
            LOG_E("the id is not a pwm id");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        ledc_channel_t ch = hal_esp32_pwm_channel(id);
        if (ch == LEDC_CHANNEL_MAX) {
            LOG_E("the pwm id not register");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        plum_u32 duty = ledc_get_duty(LEDC_LOW_SPEED_MODE, ch);
        rc            = duty / 4096;

        PLUM_UNUSED(duty);
    } while (0);

    return (rc);
}

plum_s32 plum_hal_pwm_duty_set(plum_u32 id, plum_u32 duty)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_PWM(id)) {
            LOG_E("the id is not a pwm id");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        ledc_channel_t ch = hal_esp32_pwm_channel(id);

        if (ch == LEDC_CHANNEL_MAX) {
            LOG_E("the pwm id not register");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        plum_u32 value = 40.96 * duty;
        rc             = ledc_set_duty(LEDC_LOW_SPEED_MODE, ch, value);
        if (rc) {
            LOG_E("ledc_set_duty err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        rc = ledc_update_duty(LEDC_LOW_SPEED_MODE, ch);
        if (rc) {
            LOG_E("ledc_update_duty err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
